﻿/**
   设备量测，只有名称，信息不全
**/
$(function($){
    getComponentList();//初始话下拉列表需要的数据
    initComponentGrid();//初始化表格控件
	initMeasureGrid('yc');
	 initMeasureGrid('yx');
	 initMeasureGrid('yk');
	initMeasureGrid('yt');
	/*
$("#listtest").jqxDropDownList({
                              source: getComponentList(),theme:theme, displayMember: 'component_name', valueMember: 'component_type', width: 100, height: 20
                          });*/
	});

	/////////////**********基本设备表初始化***********/////////////
function initComponentGrid() {

   
    var source = {
        dataType : "csv",
        dataFields : [{
            name : "component_no",
            type : "number"
        }, // Possible values: 'string', 'date', 'number', 'float', 'int', 'bool'.
          {
            name : "component_capacity",
            type : "number"
        }, {
            name : "component_identify",
            type : "string"
        }, {
            name : "component_factory",
            type : "string"
        }],
       url:"compoent_detail.csv",
        id : "id",
                addRow: function (rowID, rowData, position, commit) {
                    // synchronize with the server - send insert command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    // you can pass additional argument to the commit callback which represents the new ID if it is generated from a DB.
                    commit(true);
                },
                updateRow: function (rowID, rowData, commit) {
                    // synchronize with the server - send update command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    commit(true);
                },
                deleteRow: function (rowID, commit) {
                    // synchronize with the server - send delete command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    commit(true);
                }	
    };

    var dataAdapter = new $.jqx.dataAdapter(source, {
        autoBind: true,  
		async:false, 
        loadComplete : function() {

        }
    });
    // create jqxTreeGrid.
    // var filterrenderer = function (row, column, cellvalue) {
    //     var value = cellvalue.filter1+cellvalue.filter1_arg+cellvalue.filter_n+cellvalue.filter2+cellvalue.filter2_arg;
    //    return '<span style="margin: 6px 3px; font-size: 12px; float: right; font-weight: bold;">' + cellvalue + '</span>';
    // }
  var table=  $("#component_detail").jqxGrid({
        source : dataAdapter,
        // altRows: true,
        width : '99.5%',
        height : '200',
   
        editable: true,
        theme : theme,
		selectionMode: "singleRow" ,
	    ready: function()//设置被选择的项
		{  

		},

        columns : [{
            text : "序号",
            align : "center",
			columntype: 'textbox',
            dataField : "component_no",
             
            editable : true
        } , {
            text : "容量",
            cellsAlign : "center",
            align : "center",
			 
            dataField : "component_capacity",
             
			editable : true ,cellsrenderer: function(row, field, value) { 
 
  
			  return value&&(value+"MVar");
			  }  ,validation: function (cell, value) {
                          if (value > 1000 || value <=0) return { message: "设备容量必须在0-1000 范围内！", result: false };
                          return true;
                      }          
        }, {
            text : "型号标识",
            cellsAlign : "center",
            align : "center",
			 
            dataField : "component_identify",
         
			editable : true  
			,validation: function (cell, value) {
                           if (value.length < 3) return { message: "型号标识必须大于 3 个字符！", result: false };
                          return true;
                      }         
        }
		, {
            text : "厂家",
    
		    cellsAlign : "center",
            align : "center",
            dataField : "component_factory",
         
			editable : true ,validation: function (cell, value) {
                           if (value.length < 2) return { message: "厂家必须大于2 个字符！", result: false };
                          return true;
                      }          
        }]

    });
	
//===========================================
$("#tool_add").on("click",function(){
	var commit = table.jqxGrid('addrow', null, {});
	});
	
$("#tool_delete").on("click",function(){
		     var selection = table.jqxGrid('getSelection');
		  if(selection.length>0)
		 var commit =  table.jqxGrid('deleterow', selection[0].uid);
});

}
/////////////********** 设备量测表初始化***********/////////////
function initMeasureGrid(type) {
	var componentUrl={"yc":"compoentYC.csv","yx":"compoentYX.csv","yk":"compoentYK.csv","yt":"compoentYT.csv"};
    var source = {
        dataType : "csv",
        dataFields : [{
            name : "measureNo",
            type : "number"
        }, // Possible values: 'string', 'date', 'number', 'float', 'int', 'bool'.
        {
            name : "measureName",
            type : "string"
        } ],
        url:componentUrl[type],
        id : "id",
                addRow: function (rowID, rowData, position, commit) {
                    // synchronize with the server - send insert command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    // you can pass additional argument to the commit callback which represents the new ID if it is generated from a DB.
					//alert(rowID+"  "+rowData+"  "+position+"  ");
                    commit(true);
                },
                updateRow: function (rowID, rowData, commit) {
                    // synchronize with the server - send update command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
					//alert(rowID+"  "+rowData+"  ");
                    commit(true);
                },
                deleteRow: function (rowID, commit) {
                    // synchronize with the server - send delete command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failed.
                    commit(true);
                }

    };

    var dataAdapter = new $.jqx.dataAdapter(source, {
        loadComplete : function() {

        }
    });
    // create jqxTreeGrid.
    // var filterrenderer = function (row, column, cellvalue) {
    //     var value = cellvalue.filter1+cellvalue.filter1_arg+cellvalue.filter_n+cellvalue.filter2+cellvalue.filter2_arg;
    //    return '<span style="margin: 6px 3px; font-size: 12px; float: right; font-weight: bold;">' + cellvalue + '</span>';
    // }
  var table=  $("#"+type+" #table").jqxGrid({
        source : dataAdapter,
        // altRows: true,
        width : '99.5%',
        height : '340',
   
         editable: true,
        theme : theme,
		selectionMode: "singleRow" ,
	    ready: function()//设置被选择的项
		{  

		},

        columns : [{
            text : "编号",
            align : "center",
            dataField : "measureNo",
             
            editable : true ,validation: function (cell, value) {
                          
                          return true;
                      }  
        }, {
            text : "名称",
 
            align : "center",
            dataField : "measureName",
            
			editable : true  ,validation: function (cell, value) {
                          
                          return true;
                      }            
        } ]

    });
	//====================设备量测修改====================
	//==================================================
	//===================添加
	$("#"+type+" .measure_add").on("click",function(){
		//如果限定rowcount不能编辑，如果删除之前的row然后在添加row就有可能与之前的row重复
		//  var rowscount = table.jqxGrid('getdatainformation').rowscount;
		//var dataRow={"measureNo":rowscount+1,"measureName":''};
		var commit = table.jqxGrid('addrow', null, {});
	});
	//===================删除
	$("#"+type+" .measure_delete").on("click",function(){
	// var selectedrowindex = table.jqxDataTable('getselectedrowindex');
	     var selection = table.jqxGrid('getSelection');
		  if(selection.length>0)
		 var commit =  table.jqxGrid('deleterow', selection[0].uid);
	  //selection[i];
   //  var rowscount = table.jqxDataTable('getdatainformation').rowscount;
   //  if (selectedrowindex >= 0 && selectedrowindex < rowscount) {//确保当前row的有效性
    //     var id = table.jqxDataTable('getrowid', selectedrowindex);
    //     var commit = table.jqxDataTable('deleterow', id);
   //  }
	});
	
}